<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="打卡地点" prop="signInSite">
        <el-input v-model="dataForm.signInSite" placeholder=""></el-input>
      </el-form-item>

      <el-form-item label="是否外出" prop="isOut">
        <!-- <el-input v-model="dataForm.isOut" placeholder=""></el-input> -->
        <el-radio v-model="dataForm.isOut" :label="1">是</el-radio>
        <el-radio v-model="dataForm.isOut" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="外出地点" v-if="dataForm.isOut == 1" prop="outSite">
        <el-input v-model="dataForm.outSite" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="外出轨迹" v-if="dataForm.isOut == 1" prop="out_track">
        <el-input type="textarea" v-model="dataForm.out_track" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="当日体温" prop="temperature">
        <el-input-number
          :min="35"
          size="mini"
          v-model="dataForm.temperature"
        ></el-input-number>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        signInTime: '',
        signInSite: '',
        status: '',
        isOut: '',
        outSite: '',
        out_track: '',
        temperature: '',
        userId: '',
        updateTime: '',
        createTime: ''
      },
      dataRule: {
        signInTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        signInSite: [{ required: true, message: '不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '不能为空', trigger: 'blur' }],
        isOut: [{ required: true, message: '不能为空', trigger: 'blur' }],
        outSite: [{ required: true, message: '不能为空', trigger: 'blur' }],
        out_track: [{ required: true, message: '不能为空', trigger: 'blur' }],
        temperature: [{ required: true, message: '不能为空', trigger: 'blur' }],
        userId: [{ required: true, message: '不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        createTime: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    userId: {
      get () {
        return this.$store.state.user.id
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/signinrecord/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.signInTime = data.signInRecord.signInTime
              this.dataForm.signInSite = data.signInRecord.signInSite
              this.dataForm.status = data.signInRecord.status
              this.dataForm.isOut = data.signInRecord.isOut
              this.dataForm.outSite = data.signInRecord.outSite
              this.dataForm.out_track = data.signInRecord.out_track
              this.dataForm.temperature = data.signInRecord.temperature
              this.dataForm.userId = data.signInRecord.userId
              this.dataForm.updateTime = data.signInRecord.updateTime
              this.dataForm.createTime = data.signInRecord.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/signinrecord/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              // 'signInTime': this.dataForm.signInTime,
              signInSite: this.dataForm.signInSite,
              // 'status': this.dataForm.status,
              isOut: this.dataForm.isOut,
              outSite: this.dataForm.outSite,
              out_track: this.dataForm.out_track,
              temperature: this.dataForm.temperature.toString(),
              userId: this.userId
            
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
